using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Browser;
using System.Windows.Controls;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using VS.Infrastructure;
using System;

namespace VS.App
{
    public partial class Shell : UserControl
    {
        private List<ModuleInfo> _loadedModules = new List<ModuleInfo>();

        private ViewNavigationInformation _lastViewNavigationInformation = new ViewNavigationInformation()
        {
            ModuleName = "main",
            RegionName = RegionNames.MainRegion,
            ViewName = "main"
        };

        public IModuleManager ModuleManager { get; private set; }

        public IRegionManager RegionManager { get; private set; }

        public Shell(IModuleManager moduleManager, IRegionManager regionManager)
        {
            InitializeComponent();

            ModuleManager = moduleManager;
            RegionManager = regionManager;

            ModuleManager.LoadModuleCompleted += (s, a) =>
            {
                if (a.Error == null)
                {
                    _loadedModules.Add(a.ModuleInfo);

                    if (a.ModuleInfo.ModuleName == _lastViewNavigationInformation.ModuleName)
                        RegionManager.RequestNavigate(_lastViewNavigationInformation.RegionName, _lastViewNavigationInformation.ViewName);
                }
            };

            Loaded += new System.Windows.RoutedEventHandler(Shell_Loaded);
        }

        private void Shell_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            TryNavigate();
        }

        private bool TryNavigate()
        {
            string[] parameters = new string[] { };
            string[] documentUriParts = HtmlPage.Document.DocumentUri.ToString().Split(new char[] { '/', '#' });

            if (documentUriParts.Contains("navigate"))
            {
                _lastViewNavigationInformation.ModuleName = documentUriParts[3];
                _lastViewNavigationInformation.RegionName = documentUriParts[4];
                _lastViewNavigationInformation.ViewName = documentUriParts[5];
                parameters = documentUriParts[6].Split(new char[] { ',' });

                StringBuilder sb = new StringBuilder(_lastViewNavigationInformation.ViewName);
                if (parameters.Length > 0) sb.Append("?");
                for (int i = 0; i < parameters.Length; i++) sb.Append(string.Format("p{0}={1}{2}", i, parameters[i], i == parameters.Length - 1 ? string.Empty : "&"));
                _lastViewNavigationInformation.ViewName = sb.ToString();
            }

            if (_loadedModules.Any(x => x.ModuleName == _lastViewNavigationInformation.ModuleName))
            {
                RegionManager.RequestNavigate(_lastViewNavigationInformation.RegionName, _lastViewNavigationInformation.ViewName);
                return true;
            }
            else
            {
                ModuleManager.LoadModule(_lastViewNavigationInformation.ModuleName);
                return false;
            }
        }

    }
}
